package linuxbox.filelib.servlet;

import java.util.Calendar;
import javax.servlet.http.*;
import org.apache.log4j.Logger;

import linuxbox.common.*;
import linuxbox.filelib.logic.*;

/**
 * <p>Title: Linuxbox</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Xia,Xunfan
 * @version 1.0
 */

public class FileLibSearchAction
    extends Action
{
  protected static Logger logger = Logger.getLogger(FileLibSearchAction.class.
      getName());
  protected static FileLibHome home = FileLibHome.getInstance();

  protected static String searchResultJSP = null;

  public void doAction(HttpSession session, BoxSession aBoxSession,
                       HttpServletRequest req, HttpServletResponse resp) throws
      Exception
  {
    SearchEntry criteria = (SearchEntry) session.getAttribute(
        "filelib.SEARCHENTRY");
    if (criteria == null)
    {
      criteria = new SearchEntry();
      session.setAttribute("filelib.SEARCHENTRY", criteria);
    }

    String str = (String) req.getParameter("SubmitSort");
    if (str != null)
    {
      str = (String) req.getParameter("SORTBY");
      if ( str != null && str.length() > 0 )
        criteria.sortBy = str;

      str = (String) req.getParameter("ASC");
      if ( str != null && str.length() > 0 )
        criteria.sortAsc = new Boolean(str).booleanValue();

      criteria.resultIt = home.search(criteria, aBoxSession.getID());
      sendFile(searchResultJSP, req, resp);
      return;
    }

    criteria.resultIt = null;
    criteria.searchText = "";
    criteria.validationMsgs.clear();

    str = (String) req.getParameter("SubmitQuickSearch");
    if (str != null)
    {
      criteria.searchName = true;
      criteria.searchFoldername = true;
      criteria.searchFilename = true;
      criteria.searchByTime = false;
      criteria.searchBySize = false;

      str = (String) req.getParameter("SEARCH_TEXT");
      if (str != null && str.length() > 0)
      {
        criteria.searchText = str;
        criteria.resultIt = home.search(criteria, aBoxSession.getID());
      }
      sendFile(searchResultJSP, req, resp);
      return;
    }

    str = (String) req.getParameter("SubmitSearch");
    if (str != null)
    {
      if (collectCriteria(session, req, criteria))
        criteria.resultIt = home.search(criteria, aBoxSession.getID());
      sendFile(searchResultJSP, req, resp);
      return;
    }
    sendError("No parameters for FileLibSearchAction!", req, resp);
  }

  public boolean collectCriteria(HttpSession session, HttpServletRequest req, SearchEntry criteria)
  {
    boolean isValid = true;

    String str = req.getParameter("SEARCH_TEXT");
    if (str != null)
      criteria.searchText = str;

    str = req.getParameter("SEARCH_BY_NAME");
    int tmp = 0;
    if ( str != null )
      tmp = Integer.parseInt(str);
    if ( tmp == 0 )
    {
      criteria.searchName = false;
    }
    else
    {
      criteria.searchName = true;
    }

    tmp = 0;
    str = req.getParameter("SEARCH_BY_FILENAME");
    if ( str != null )
      tmp = Integer.parseInt(str);
    if ( tmp == 0 )
    {
      criteria.searchFilename = false;
    }
    else
    {
      criteria.searchFilename = true;
    }

    tmp = 0;
    str = req.getParameter("SEARCH_BY_FOLDERNAME");
    if ( str != null )
      tmp = Integer.parseInt(str);
    if ( tmp == 0 )
    {
      criteria.searchFoldername = false;
    }
    else
    {
      criteria.searchFoldername = true;
    }

    tmp = 0;
    str = req.getParameter("SEARCH_BY_TIME");
    if ( str != null )
      tmp = Integer.parseInt(str);
    if ( tmp == 0 )
    {
      criteria.searchByTime  = false;
    }
    else
    {
      criteria.searchByTime = true;

      Calendar fromCal = Calendar.getInstance();
      Calendar toCal = Calendar.getInstance();
      int tmpYear = fromCal.get(Calendar.YEAR);
      int tmpMonth = fromCal.get(Calendar.MONTH);
      int tmpDay = fromCal.get(Calendar.DAY_OF_MONTH);

      str = req.getParameter("FROM_YEAR");
      if ( str != null )
        tmpYear = Integer.parseInt(str);
      str = req.getParameter("FROM_MONTH");
      if ( str != null )
        tmpMonth = Integer.parseInt(str);
      str = req.getParameter("FROM_DAY");
      if ( str != null )
        tmpDay = Integer.parseInt(str);

      fromCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
      fromCal.set(Calendar.MILLISECOND, 0);

      criteria.modifiedFrom = fromCal;

       str = req.getParameter("TO_YEAR");
       if ( str != null )
         tmpYear = Integer.parseInt(str);
       str = req.getParameter("TO_MONTH");
       if ( str != null )
         tmpMonth = Integer.parseInt(str);
       str = req.getParameter("TO_DAY");
       if ( str != null )
         tmpDay = Integer.parseInt(str);

       toCal.set(tmpYear, tmpMonth, tmpDay, 0, 0, 0);
       toCal.set(Calendar.MILLISECOND, 0);
       criteria.modifiedTo = toCal;
       if ( fromCal.after(toCal))
       {
         isValid = false;
         criteria.validationMsgs.add("To time is greater than from time");
       }
    }

    tmp = 0;
    str = req.getParameter("SEARCH_BY_SIZE");
    if ( str != null )
      tmp = Integer.parseInt(str);
    if ( tmp == 0 )
    {
      criteria.searchBySize  = false;
    }
    else
    {
      criteria.searchBySize = true;

      tmp = 0;
      str = req.getParameter("SEARCH_SIZE_GREATER");
      if (str != null)
        tmp = Integer.parseInt(str);
      if (tmp == 0)
      {
        criteria.isSizeGreater = true;
      }
      else
      {
        criteria.isSizeGreater = false;
      }

      str = req.getParameter("SEARCH_SIZE");
      if (str != null)
        criteria.size = str;
      tmp = 0;
      if (str != null)
        tmp = Integer.parseInt(str);
      if (tmp == 0)
      {
        isValid = false;
        criteria.validationMsgs.add("Invalid size!");
      }
      str = req.getParameter("SEARCH_SIZE_UNIT");
      criteria.sizeUnit = Integer.parseInt(str);
    }
    if (isValid)
    {
      if (!criteria.searchBySize && !criteria.searchByTime)
      {
        if ((criteria.searchText==null ||criteria.searchText.length() ==0)
        || (!criteria.searchName && ! criteria.searchFilename && ! criteria.searchFoldername))
        {
          isValid = false;
          criteria.validationMsgs.add("No criteria sepcified for search!");
        }
      }
    }

    return isValid;
  }

  public void setSearchResultJSP(String str)
  {
    searchResultJSP = str;
  }
}
